package com.example.demo.controller;

import com.example.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/getRedisValue")
    public String getRedisValue(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }

    @RequestMapping("/setRedisValue")
    public String setRedisValue(String key, String value){
        stringRedisTemplate.opsForValue().set(key, value);

        System.out.println(stringRedisTemplate.opsForValue().get(key));
        return "success";
    }

    @RequestMapping("/setRedisObject")
    public String setRedisObject(){
        User user = new User();
        user.setName("小明");
        user.setPassword("123456");
        user.setAge(16);
        user.setBirchday(new Date());

        redisTemplate.opsForValue().set("user", user);

        User user1 = (User)redisTemplate.opsForValue().get("user");

        System.out.println(user1.toString());

        Map<String, String> map = new HashMap<>();
        map.put("aa","aaa");
        map.put("bb","bbb");
        map.put("ccc","aaccca");
        map.put("dd","ddd");

        redisTemplate.opsForValue().set("map", map);

        Map<String, String> map1 = (Map<String, String>) redisTemplate.opsForValue().get("map");

        System.out.println(map1.get("aa"));

        return "success";
    }

    @RequestMapping("/test")
    public String testa(){
        List<String> list = new ArrayList<>();
        list.add("aaaaa");
        list.add("bbbbb");
        list.add("ccccc");
        list.add("ddddd");

        stringRedisTemplate.opsForList().leftPushAll("testList", list);
        long size = stringRedisTemplate.opsForList().size("testList");
        List<String> list1 = stringRedisTemplate.opsForList().range("testList",0,-1);

        return "llll";
    }

}
